package com.dai.zhenfujin.my;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.widget.Button;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.TextView;

import com.dai.zhenfujin.Constants.Constants;
import com.dai.zhenfujin.R;
import com.dai.zhenfujin.base.BaseActivity;
import com.dai.zhenfujin.bean.ProListBean;
import com.dai.zhenfujin.http.CallBack;
import com.dai.zhenfujin.http.HttpClient;
import com.dai.zhenfujin.utils.UIUtils;

import java.util.HashMap;
import java.util.Map;

public class ResetPasswordActivity extends BaseActivity {

    private EditText et_password,et_new_password;
    private Button bt_save;
    private FrameLayout fl_img;
    private TextView tv_title;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_reset_password);
        initView();
    }

    private void initView() {
        et_password = findViewById(R.id.et_password);
        et_new_password = findViewById(R.id.et_new_password);
        bt_save = findViewById(R.id.bt_save);
        fl_img = findViewById(R.id.fl_img);
        tv_title = findViewById(R.id.title_textview);
        tv_title.setText("修改登录密码");
        bt_save.setOnClickListener(v -> updatePwd());

        fl_img.setOnClickListener(v -> finish());


    }

    private void updatePwd() {
        if (TextUtils.isEmpty(et_password.getText().toString().trim())){
            UIUtils.showToastSafe("请输入旧密码");
            return;
        }
        if (TextUtils.isEmpty(et_new_password.getText().toString().trim())){
            UIUtils.showToastSafe("请输入6-12位新密码");
            return;
        }
        if (et_new_password.getText().toString().trim().length() < 6){
            UIUtils.showToastSafe("请输入6-12位新密码");
            return;
        }

        Map<String, Object> params = new HashMap<>();
        Map<String, String> params2 = new HashMap<>();
        params2.put("oldPwd",UIUtils.Md5(et_password.getText().toString().trim()));
        params2.put("newPwd", UIUtils.Md5(et_new_password.getText().toString().trim()));
        params.put("req_bd", params2);
        HttpClient.post(this, Constants.UPDATEPWD, params, new CallBack<ProListBean>() {
            @Override
            public void onSuccess(ProListBean resultBean) {
                UIUtils.showToastSafe("修改密码成功");
                finish();
            }
        });
    }
}
